package com.pi.shisan.web.controller;

import com.pi.shisan.service.config.DestroyListener;
import com.pi.shisan.service.constants.DndModeTypeEnum;
import com.pi.shisan.service.dto.CityDTO;
import com.pi.shisan.service.dto.LedDndModeSettingDTO;
import com.pi.shisan.service.dto.Result;
import com.pi.shisan.service.dto.WeatherInfoDTO;
import com.pi.shisan.service.schedule.job.SecondNotifyJob;
import com.pi.shisan.service.service.CityService;
import com.pi.shisan.service.service.LedDndModeSettingService;
import com.pi.shisan.service.service.VolumeSettingService;
import com.pi.shisan.service.service.WeatherService;
import com.pi.shisan.service.util.AppUtil;
import com.pi.shisan.service.util.ComponentManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/setting")
public class SettingController {

    @Autowired
    private DestroyListener destroyListener;

    @Autowired
    private VolumeSettingService volumeSettingService;

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private CityService cityService;

    @Autowired
    ComponentManager componentManager;

    @Autowired
    private LedDndModeSettingService ledDndModeSettingService;

    @Autowired
    private SecondNotifyJob secondNotifyJob;

    @PostMapping("/volume/{num}")
    public Result<Void> setVolume(@PathVariable("num") Integer num) throws Exception{
        volumeSettingService.setVolume(num);
        return Result.ok();
    }

    @GetMapping("/volume")
    public Result<Integer> getVolume() throws Exception{
        return Result.ok(volumeSettingService.getVolume());
    }

    @GetMapping("/get_weather")
    public Result<WeatherInfoDTO> weather() throws Exception{
        return Result.ok(weatherService.getWeatherInfo());
    }

    @GetMapping("/getCityList/{pid}")
    public Result<List<CityDTO>> cityList(@PathVariable("pid") Long pid) throws Exception{
        return Result.ok(cityService.getCityList(pid));
    }

    @PostMapping("/setMyCity/{id}")
    public Result<Void> setMyCity(@PathVariable("id") Long id) throws Exception{
        cityService.setMyCity(id);
        return Result.ok();
    }

    @PostMapping("/shutdown_system")
    public Result<Void> shutdown() throws Exception{
        new Thread(()->{
            destroyListener.setToShutdownSystem();
            SpringApplication.exit(AppUtil.getApplicationContext());
        }).start();
        return Result.ok();
    }

    @PostMapping("/light_level/{level}")
    public Result<Void> lightLevel(@PathVariable("level") Integer level) {
        componentManager.setupLightLevel(level);
        return Result.ok();
    }

    @GetMapping("/light_level")
    public Result<Integer> lightLevel() {
        return Result.ok(componentManager.getLightLevel());
    }

    /**
     * 更新免打扰时间
     * @param param
     */
    @PostMapping("/openAndUpdateDnd")
    public Result<Void> updateMode(@RequestBody LedDndModeSettingDTO param) {
        if (param.getDndStartTime() == null || param.getDndEndTime() == null || DndModeTypeEnum.getByType(param.getDndType()) == null) {
            return Result.err("参数错误");
        }
        if (param.getDndStartTime() < 0 || param.getDndStartTime() > 1440) {
            return Result.err("开始时间应该在24小时之内");
        }
        if (param.getDndEndTime() < 0 || param.getDndEndTime() > 1440) {
            return Result.err("结束时间应该在24小时之内");
        }
        if (Objects.equals(param.getDndStartTime(), 1440)) {
            param.setDndStartTime(0);
        }
        if (Objects.equals(param.getDndEndTime(), 1440)) {
            param.setDndEndTime(0);
        }
        ledDndModeSettingService.updateMode(param);
        if (Objects.equals(param.getDndType(), DndModeTypeEnum.LED.getType())) {
            secondNotifyJob.refreshLedDnd();
        } else if (Objects.equals(param.getDndType(), DndModeTypeEnum.CLOCK_NOTIFY.getType())) {
            secondNotifyJob.refreshClockDnd();
        }
        return Result.ok();
    }

    /**
     * 清除免打扰设置
     * @param type
     */
    @PostMapping("/clearDnd/{type}")
    public Result<Void> clearMode(@PathVariable("type") Integer type) {
        ledDndModeSettingService.clearMode(type);
        if (Objects.equals(type, DndModeTypeEnum.LED.getType())) {
            secondNotifyJob.refreshLedDnd();
        } else if (Objects.equals(type, DndModeTypeEnum.CLOCK_NOTIFY.getType())) {
            secondNotifyJob.refreshClockDnd();
        }
        return Result.ok();
    }

    /**
     * 获取配置
     * @param type
     * @return
     */
    @GetMapping("/dndInfo/{type}")
    public Result<LedDndModeSettingDTO> getSettingByType(@PathVariable("type") Integer type) {
        return Result.ok(ledDndModeSettingService.getSettingByType(type));
    }
}
